package com.heima.behavior.service.Impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.LikeBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikeBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: heima-leadnews
 * @description:
 * @author: lydms
 * @create: 2024-04-12 13:51
 **/

@Service
@Slf4j
@Transactional
public class LikeBehaviorServiceImpl implements LikeBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 喜欢行为
     *
     * @param likeBehaviorDto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikeBehaviorDto likeBehaviorDto) {
        //1.判断参数
        if (likeBehaviorDto == null || likeBehaviorDto.getArticleId() ==null || checkParam(likeBehaviorDto) ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if(user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //实时消息
        UpdateArticleMess updateArticleMess = new UpdateArticleMess();
        updateArticleMess.setArticleId(likeBehaviorDto.getArticleId());
        updateArticleMess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        //3.redis存储喜欢行为
        //点赞状态  0 点赞   1 取消点赞
        if (likeBehaviorDto.getOperation() == 0) {
            Object obj = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + likeBehaviorDto.getArticleId().toString(), user.getId().toString());
            if (obj != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已点赞");
            }
            // 保存当前key
            log.info("保存当前key:{} ,{}, {}", likeBehaviorDto.getArticleId(), user.getId(), likeBehaviorDto);
            cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR + likeBehaviorDto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(likeBehaviorDto));
            updateArticleMess.setAdd(1);
        } else {
            // 删除当前key
            log.info("删除当前key:{}, {}", likeBehaviorDto.getArticleId(), user.getId());
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR + likeBehaviorDto.getArticleId().toString(), user.getId().toString());
            updateArticleMess.setAdd(-1);
        }

        //kafka发送实时消息
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(updateArticleMess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //判断参数
    private boolean checkParam(LikeBehaviorDto dto) {
        if (dto.getType() > 2 || dto.getType() < 0 || dto.getOperation() > 1 || dto.getOperation() < 0) {
            return true;
        }
        return false;
    }
}
